package com.pocketmenu.persistence;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

import com.pocketmenu.business.Constants;

public class UserProfileDataSource {

	// Database fields
		  private SQLiteDatabase database;	  
		  
		  private SQLiteHelper dbHelper;
		  private String[] allColumns = 
			  { SQLiteHelper.COLUMN_ID,
				SQLiteHelper.COLUMN_USER_PROFILE_NAME,
				SQLiteHelper.COLUMN_USER_PROFILE_EMAIL,
				SQLiteHelper.COLUMN_USER_PROFILE_PASSWORD,
				SQLiteHelper.COLUMN_USER_PROFILE_AGE,		
				SQLiteHelper.COLUMN_USER_PROFILE_ADDRESS,
				SQLiteHelper.COLUMN_USER_PROFILE_SEX};

		  public UserProfileDataSource(Context context) {
		    dbHelper = new SQLiteHelper(context);
		  }

		  public void open() throws SQLException {
		    database = dbHelper.getWritableDatabase();	    
		  }

		  public void close() {
		    dbHelper.close();
		  }

		  public UserProfile create(String name, String email, String password, String age, String address, String sex) {
		    ContentValues values = new ContentValues();
		    values.put(SQLiteHelper.COLUMN_USER_PROFILE_NAME, name);
		    values.put(SQLiteHelper.COLUMN_USER_PROFILE_EMAIL, email);
		    values.put(SQLiteHelper.COLUMN_USER_PROFILE_PASSWORD, password);
		    values.put(SQLiteHelper.COLUMN_USER_PROFILE_AGE, age);
		    values.put(SQLiteHelper.COLUMN_USER_PROFILE_ADDRESS, address);
		    values.put(SQLiteHelper.COLUMN_USER_PROFILE_SEX, sex);		   		    
		    
		    long insertId = database.insert(SQLiteHelper.TABLE_USER_PROFILE, null,
		        values);
		    
		    Cursor cursor = database.query(SQLiteHelper.TABLE_USER_PROFILE,
		        allColumns, SQLiteHelper.COLUMN_ID + " = " + insertId, null,
		        null, null, null);
		    cursor.moveToFirst();
		    UserProfile newTemp = cursorToObject(cursor);
		    cursor.close();
		    return newTemp;
		  }

		  public void delete() {		    		    
		    database.delete(SQLiteHelper.TABLE_USER_PROFILE, SQLiteHelper.COLUMN_ID, null);
		  }		  

		  private UserProfile cursorToObject(Cursor cursor) {
			UserProfile temp = new UserProfile();
			
		    temp.setId(cursor.getInt(0));
		    temp.setName(cursor.getString(1));
		    temp.setEmail(cursor.getString(2));
		    temp.setPassword(cursor.getString(3));
		    temp.setAge(cursor.getString(4));
		    temp.setAddress(cursor.getString(5));
		    temp.setSex(cursor.getString(6));			
			
		    return temp;
		  }		  		  
		  
		  public void updateUserProfile(String name, String email, String password, String age, String address, String sex)
		  {			  			  
			  ContentValues values = new ContentValues();
			  values.put(SQLiteHelper.COLUMN_USER_PROFILE_NAME, name);
			    values.put(SQLiteHelper.COLUMN_USER_PROFILE_EMAIL, email);
			    values.put(SQLiteHelper.COLUMN_USER_PROFILE_PASSWORD, password);
			    values.put(SQLiteHelper.COLUMN_USER_PROFILE_AGE, age);
			    values.put(SQLiteHelper.COLUMN_USER_PROFILE_ADDRESS, address);
			    values.put(SQLiteHelper.COLUMN_USER_PROFILE_SEX, sex);
			  			  
			  database.update(SQLiteHelper.TABLE_USER_PROFILE, values, null, null);  
		  }
		  
		  public UserProfile getUserProfile()
		  {		  
			  Cursor cursor = database.query(SQLiteHelper.TABLE_USER_PROFILE, allColumns, null, null, null, null, null);
			  UserProfile order = null;
			  
			  if(cursor.getCount() > 0)
			  {
				  cursor.moveToFirst();
				  order = cursorToObject(cursor);
			  }
				  
			  cursor.close();
			  
			  return order;
		  }
}
